﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Movie.DAL
{
  public  class OrderDAL
    {
      
      public static int InsertOrder(Modal.Order order)
      {
          string sql =string.Format("insert into Orders values({0},'{1}',{2},'{3}',{4},'{5}',{6},{7},{8},{9})",order.ScheduleID,order.Mname,order.Price,order.Mpicture,order.Num,DateTime.Now,order.CustID,order.Ooff?1:0,order.Orevoke?1:0,order.OPaid?1:0);
            
          int orderId = Convert.ToInt32(sqlHelper2.ExecuteNonQuery(sql));

          for (int i = 0; i < order.OrderSeats.Count; i++)
          {
              sql = string.Format("insert into OrderSeat values({0},{1})", order.OrderSeats[i].SeatID, orderId);

              int result = sqlHelper2.ExecuteNonQuery(sql);          
          }
          return 0;

      }
      public static  List<Modal.Order> GetOrder(int CustID) 
      {
          string sql = "select * from Orders where CustID=" + CustID;
          SqlDataReader reder = sqlHelper2.ExcuteReader(sql);
          List<Modal.Order> list = new List<Modal.Order>();
          while (reder.Read())
          {
              Modal.Order orde = new Modal.Order();
              orde.OrderID =Convert.ToInt32(reder["OrderID"]);
              orde.ScheduleID=Convert.ToInt32(reder["ScheduleID"]);
              orde.Mname=reder["Mname"].ToString();
              orde.Price=Convert.ToInt32(reder["Price"]);
              orde.Mpicture=reder["Mpicture"].ToString();
              orde.Num=Convert.ToInt32(reder["Num"]);
              orde.OrderDate=Convert.ToDateTime(reder["OrderDate"]);
              orde.OPaid=Convert.ToBoolean(reder["OPaid"]);
              list.Add(orde);
          }

          return list;
      
      }

    }
}
